# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Stefan Tatschner <stefan@rumpelsepp.org>

pkgname=gron
pkgver=0.7.1
pkgrel=2
pkgdesc='Makes JSON greppable'
arch=('loong64' 'x86_64')
url='https://github.com/tomnomnom/gron'
license=('MIT')
depends=('glibc')
makedepends=('git' 'go')
options=('!lto')
_commit='badf401da553eb41b7ffde4be6a64809ed0ed846'
source=("$pkgname::git+https://github.com/tomnomnom/gron.git#commit=$_commit")
b2sums=('SKIP')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgname"

  # create directory for build output
  mkdir build

  # download dependencies
  go mod download
}

build() {
  cd "$pkgname"

  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"

  go build -v \
    -trimpath \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-linkmode external -extldflags $LDFLAGS" \
    -o build \
    .
}

check() {
  cd "$pkgname"

  go test -v ./...
}

package() {
  cd "$pkgname"

  # binary
  install -vDm755 -t "$pkgdir/usr/bin" build/gron

  # shell completion
  install -vDm644 -t "$pkgdir/usr/share/fish/vendor_completions.d" completions/gron.fish

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" {README,ADVANCED}.mkd

  # license
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE
}
